var parentPage = window.parent
var box = window.parent.document.querySelector("#bodyfilebox");
var pdf = document.querySelector("#viewerContainer");

function GetRequest() {
    var urldata = decodeURIComponent(location.search).substr(1).split('?')[1].split('&')
    var obj = {}
    for (var i = 0; i < urldata.length; i++) {
        var a = urldata[i].split('=')
        obj[a[0]] = a[1]
    }
    return obj;
}

var query = GetRequest()

var res = /^[0-9]+.?[0-9]*/;//判断字符串是否为数字//判断正整数/[1−9]+[0−9]∗]∗/ 
var timer = setInterval(function() {
    // var scaleSelect = document.querySelector('#scaleSelect')
    var pageNumber = document.querySelector("#pageNumber")

    var defaultSize;

    if (query['widthType'] == 'auto') {
        defaultSize = 'auto'
    } else if (query['widthType'] == 'pageActual') {
        defaultSize = 'page-actual'
    } else if (query['widthType'] == 'pageFit') {
        defaultSize = 'page-fit'
    } else if (query['widthType'] == 'pageWidth') {
        defaultSize = 'page-width'
    } else if (res.test(Number(query['widthType']))) {
        defaultSize = Number(query['widthType']) / 100
    } else {
        defaultSize = 1
    }

    if(pageNumber&&pageNumber.max&&pageNumber.max>0){
        var eventBus = window.PDFViewerApplication.eventBus;
        // scaleSelect.selectedIndex=2
        eventBus.dispatch('scalechanged', {
            source: this,
            value: defaultSize
        });
        window.clearInterval(timer)
    }
  },1000)

if(query.viewType === 'mobile'){
    document.querySelector('#toolbarContainer').style.background = '#2A517A'
    document.querySelector('#secondaryToolbar').style.background = '#2A517A'
    document.querySelector('#toolbarSidebar').style.background = '#2A517A'
    document.querySelector('#thumbnailView').style.background = '#2A517A'
    var selectOption = scaleSelect.querySelectorAll('option')
    for (var i = 0; i < selectOption.length; i++) {
        selectOption[i].style.background = '#2A517A'
    }

    document.querySelector('#secondaryDownload').style.display = 'none'
    document.querySelector('#download').style.display = 'none'
    document.querySelector('#secondaryPresentationMode').style.display = 'none'
    document.querySelector('#presentationMode').style.display = 'none'

}

var zoom;
var img;
var fullSize,originalW,originalH;
var plusBtn,minusBtn,deleteBtn;

function addImage(src, width, height,type) {
    window.setTimeout(function () {
        addImgCall(src, width, height,type);
    }, 10);
}

function addImgCall(src, width, height,type) {
    zoom = "zoom:" + imgzoom;
    var page = document.querySelector("#pageNumber").value;
    //page=totalPage;
    var container = document.querySelector(".page[data-page-number*='" + page + "']").querySelector(".textLayer")
    //moveImage(totalPage);
    container.style.opacity = 1

    if(width.indexOf('%') > -1){
        fullSize = parseInt(width) / 100
        width = 'auto'
    }

    var imgDOM = "<div class='innerimgbox'>\
                    <img id='floatingInnerImage' class='floatingImage' src='" + src + "' style='width:" + width + ";height:" + height +"' />\
                </div>"
    var imgBtn = '<div class="floatingImage-btn '+(query.viewType=="mobile"?"mobile-floatingImage-btn":"")+'" style="top:-26px;left:0">\
                    <div class="btnplus nolock">\
                        <span>+</span>\
                    </div>\
                    <div class="btnpminus nolock">\
                        <span>-</span>\
                    </div>\
                    <div class="btnlock nolock">\
                        <span>✔</span>\
                    </div>\
                    <div class="btndelte nolock">\
                        <span>×</span>\
                    </div>\
                    <div class="unlock">\
                        <span>\
                            <svg t="1608951911975" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="11771" width="400" height="400"><path d="M822.4 1016.32H196.48a83.2 83.2 0 0 1-83.2-83.2V467.2A83.2 83.2 0 0 1 196.48 384h625.92a83.2 83.2 0 0 1 83.2 83.2v465.92a83.2 83.2 0 0 1-83.2 83.2zM196.48 448a19.2 19.2 0 0 0-19.2 19.2v465.92a19.2 19.2 0 0 0 19.2 19.2h625.92a18.56 18.56 0 0 0 19.2-19.2V467.2a18.56 18.56 0 0 0-19.2-19.2z" p-id="11772"></path><path d="M285.44 441.6A32.64 32.64 0 0 1 256 409.6V259.84a252.8 252.8 0 0 1 504.96 0 32 32 0 0 1-32 32 32 32 0 0 1-32-32 188.8 188.8 0 0 0-376.96 0V409.6a32 32 0 0 1-34.56 32z" p-id="11773"></path><path d="M508.8 662.4m-88.96 0a88.96 88.96 0 1 0 177.92 0 88.96 88.96 0 1 0-177.92 0Z" p-id="11774"></path><path d="M512 855.04a32 32 0 0 1-32-32v-111.36a32 32 0 1 1 64 0v111.36a32 32 0 0 1-32 32z" p-id="11775"></path></svg>\
                        </span>\
                    </div>\
                </div>'
    var imgOut = '<div id="floatingImage" class="floatingImage-out" style="top:30%;left:50%;'+zoom+'">'+imgBtn+imgDOM+'</div>'
    
    container.innerHTML = imgOut + container.innerHTML
    var innerImg = document.querySelector("#floatingInnerImage");

    plusBtn = document.querySelector('.btnplus')
    minusBtn = document.querySelector('.btnpminus')
    deleteBtn = document.querySelector('.btndelte')
    lockBtn = document.querySelector('.btnlock')
    unlockBtn = document.querySelector('.unlock')

    img = document.querySelector("#floatingImage");
    setTimeout(function() {
        originalW = innerImg.offsetWidth
        originalH = innerImg.offsetHeight
        if(fullSize!==undefined && fullSize !== null){
            innerImg.style.width = originalW * fullSize + 'px'
            innerImg.style.height = originalH * fullSize + 'px'
        }else{
            fullSize = 1
        }
    },100)
    

    if(query.viewType !== 'mobile'){
        img.onmousedown = imgMousedown
    }else{
        imgtouchstart()
    }

    plusBtn.addEventListener('click',plusClick,true)
    minusBtn.addEventListener('click',minusClick,true)
    deleteBtn.addEventListener('click',deleteClick,true)
    lockBtn.addEventListener('click',lockClick,true)
    unlockBtn.addEventListener('click',unlockClick,true)
}
// PC移动事件
function imgMousedown(downEvent){
    var page = document.querySelector("#pageNumber").value;
    // var move_img = document.querySelector('.innerimgbox')
    // 当前页宽度
    container = document.querySelector(".page[data-page-number*='" + page + "']").querySelector(".textLayer")
    // 获取按钮
    var floatbtn = document.querySelector('.floatingImage-btn')

    // 鼠标按下时的位置
    var mousX = downEvent.clientX
    var mousY = downEvent.clientY
    // 图片到周围的边距
    var img_top = img.offsetTop * imgzoom
    var img_left = img.offsetLeft * imgzoom
    // 图片的实际宽高
    var imgW = img.offsetWidth * imgzoom
    var imgH = img.offsetHeight * imgzoom
    // 移动距离
    document.onmousemove = function (moveEvent) {
        // 计算鼠标偏移量
        var mouseMoveX = moveEvent.clientX - mousX
        var mouseMoveY = moveEvent.clientY - mousY
        // 改变图片的位置
        var imgMoveY = img_top + mouseMoveY
        var imgMoveX = img_left + mouseMoveX

        if(imgMoveX <= 0){
            imgMoveX = 0
        }else if(container.offsetWidth - imgW < imgMoveX ){
            imgMoveX = (container.offsetWidth - imgW) / imgzoom
        }else{
            imgMoveX = (img_left + mouseMoveX) / imgzoom
        }

        if(container.offsetWidth - imgMoveX < floatbtn.offsetWidth){
            floatbtn.style.left = ''
            floatbtn.style.right = '0'
        }else{
            floatbtn.style.left = '0'
            floatbtn.style.right = ''
        }

        if(imgMoveY <= 0){
            imgMoveY = 0
        }else if(container.offsetHeight - imgH < imgMoveY){
            imgMoveY = (container.offsetHeight - imgH) / imgzoom
        }else{
            imgMoveY = (img_top + mouseMoveY) / imgzoom
        }

        if(imgMoveY < floatbtn.offsetHeight){
            floatbtn.style.top = ''
            floatbtn.style.bottom = '-26px'
        }else{
            floatbtn.style.top = '-26px'
            floatbtn.style.bottom = ''
        }

        img.style.top = imgMoveY + 'px'
        img.style.left = imgMoveX + 'px'
    }

    document.onmouseup = function () {
        document.onmousemove = null
        document.onmouseup = null
    }
}

// app滑屏事件
var imgontouchstart,imgontouchmove,imgontouchend
function imgtouchstart(){
    var mousX,mousY
    var img_top,img_left
    var imgW,imgH
    // var langtouch
    imgontouchstart = function(downEvent){
        flags = true
        // 长按事件
        // langtouch = setInterval(function() {
        //     document.querySelector('.mobile-floatingImage-btn').style.display = 'flex'
        //     clearInterval(langtouch)
        // },2000)
         // 阻止页面的滑动默认事件
         document.addEventListener('touchmove', function(e) {
             if(flags)e.preventDefault()
        }, { passive: false })
        var page = document.querySelector("#pageNumber").value;
        // 当前页宽度
        container = document.querySelector(".page[data-page-number*='" + page + "']").querySelector(".textLayer")
        // 鼠标按下时的位置
        mousX = downEvent.touches[0].clientX
        mousY = downEvent.touches[0].clientY
        // 图片到周围的边距
        img_top = img.offsetTop * imgzoom
        img_left = img.offsetLeft * imgzoom
        // 图片的实际宽高
        imgW = img.offsetWidth * imgzoom
        imgH = img.offsetHeight * imgzoom
    }
    imgontouchmove = function(moveEvent) {
        if(flags){
            // clearInterval(langtouch)
            // 计算鼠标偏移量
            var mouseMoveX = moveEvent.touches[0].clientX - mousX
            var mouseMoveY = moveEvent.touches[0].clientY - mousY
            // '改变图片的位置'
            var imgMoveY = img_top + mouseMoveY
            var imgMoveX = img_left + mouseMoveX

            if(imgMoveX <= 0){
                imgMoveX = 0
            }else if(container.offsetWidth - imgW < imgMoveX ){
                imgMoveX = (container.offsetWidth - imgW) / imgzoom
            }else{
                imgMoveX = (img_left + mouseMoveX) / imgzoom
            }

            if(imgMoveY <= 0){
                imgMoveY = 0
            }else if(container.offsetHeight - imgH < imgMoveY){
                imgMoveY = (container.offsetHeight - imgH) / imgzoom
            }else{
                imgMoveY = (img_top + mouseMoveY) / imgzoom
            }
            img.style.top = imgMoveY + 'px'
            img.style.left = imgMoveX + 'px'
        }
    }
    imgontouchend = function() {
        flags = false
        // clearInterval(langtouch)
    }
    img.addEventListener('touchstart',imgontouchstart)
    img.addEventListener('touchmove',imgontouchmove)
    img.addEventListener('touchend',imgontouchend)
}
// 放大比例
function plusClick(event){
    var innerImg = document.querySelector("#floatingInnerImage");
    fullSize += 0.05
    innerImg.style.width = originalW * fullSize + 'px'
    innerImg.style.height = originalH * fullSize + 'px'
    event.stopPropagation()
}
// 缩小比例
function minusClick(event){
    var innerImg = document.querySelector("#floatingInnerImage");
    fullSize -= 0.05
    innerImg.style.width = originalW * fullSize + 'px'
    innerImg.style.height = originalH * fullSize + 'px'
    event.stopPropagation()
}
// 锁定
function lockClick(event){
    img.removeEventListener('touchstart',imgontouchstart)
    img.removeEventListener('touchmove',imgontouchmove)
    img.removeEventListener('touchend',imgontouchend)
    img.classList.add('imgdisablemove')
    img.onmousedown = null
    img = null
    parentPage.postMessage('lock', '*');
    event.stopPropagation()
}
// 解锁{
function unlockClick(event){
    img = document.querySelector("#floatingImage");
    img.classList.remove('imgdisablemove')
    img.onmousedown = imgMousedown
    parentPage.postMessage('unlock', '*');
    imgtouchstart()
    event.stopPropagation()
}
// 删除图片
function deleteClick(event){
    img.remove()
    img = null
    deleteBtn.removeEventListener('click',deleteClick,true)
    plusBtn.removeEventListener('click',plusClick,true)
    minusBtn.removeEventListener('click',minusClick,true)

    plusBtn = null
    deleteBtn = null
    minusBtn = null
    fullSize = 1
    parentPage.postMessage('delete', '*');
    event.stopPropagation()
}

// document.addEventListener('touchstart',function() {
//     document.querySelector('.mobile-floatingImage-btn').style.display = 'none'
// })

/**
 * 返回图片的XY百分比
 */
function getImgDis(){
    var imgTop , imgleft
    var containerW , containerH
    var img2 = document.querySelector("#floatingImage")
    var nowPage = document.querySelector("#pageNumber").value;
    var container = document.querySelector(".page[data-page-number*='" + nowPage + "']").querySelector(".textLayer")

    if(img2 !== null){
        containerW = container.offsetWidth / imgzoom
        containerH = container.offsetHeight / imgzoom
        imgTop = img2.offsetTop / containerH
        imgleft = img2.offsetLeft / containerW
    }
    return {nowPage,imgTop,imgleft,fullSize}
}